<%--
    Document   : book
    Author     : Thomas
--%>

<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="FeedbackPackage.FeedbackItem"%>
<%@page import="FeedbackPackage.NoFeedbackFoundException_Exception"%>
<%@page import="FeedbackPackage.FeedbackStatistics"%>
<%@page import="FeedbackPackage.Feedback"%>
<%@page import="FeedbackPackage.FeedbackService"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<jsp:useBean id="viewBookBean" class="beans_pkg.viewBookBean" scope="page"/>
<jsp:setProperty name="viewBookBean" property="*"/>

<%@ include file="header.jsp" %>

<!-- Content -->
	
<div id="container">
	<div id="content" role="main">

    <%
        Feedback feedClient = new FeedbackService().getFeedbackPort();
        bookDatabase_pkg.bookDatabaseDriver driver = new bookDatabase_pkg.bookDatabaseDriver();
        String category = viewBookBean.getCategory();
        String[] book = new String[4];
        int result = driver.databaseConnect(category);
        if(result == 0){
            //out.println("Database: " + category + " connected<br />");

            book = driver.getBook(viewBookBean.getISBN());

        }
        else{
            out.println("ERROR: Database " + category + " cannot be found!<br />");
            book[0] = "0";
            book[1] = "ERROR: Book data not found!";
            book[2] = "No description found!";
            book[3] = "£0.00";
        }
    %>

		<div id="post-0" class="post error404 not-found">
			<h1 class="entry-title"><%out.println(book[1]);%></h1>
			<div class="entry-content">
                            <p><%out.println(book[2]);%></p>
                            <hr />
                            ISBN: <%out.println(book[0]);%><br/>
                            Price: £<%out.println(book[3]);%><br/>
                            <br/>
                            <%
                            String cart2 = "";
                            
                            if(session.getAttribute( "cart" ) != null){
                                cart2 = (String) session.getAttribute( "cart" );
                            }

                            if( ! cart2.contains(book[0])){
                                out.println("<form name='addCart" + viewBookBean.getISBN() + "' role='addToCart' method='get' id='searchform' action='addToCart.jsp'>");
                                out.println("<input type='hidden' value=" + viewBookBean.getISBN() + " name='ISBN' id='ISBN' />");
                                out.println("<a href='' onclick='document.addCart" + viewBookBean.getISBN() + ".submit(); return false;'>Add Book To Cart</a>");
                                out.println("</form>");

                                String list2 = "";

                                if(session.getAttribute( "list" ) != null){
                                    list2 = (String) session.getAttribute( "list" );
                                }

                                if( ! list2.contains(book[0])){
                                    out.println("<form name='addList" + viewBookBean.getISBN() + "' role='addToCart' method='get' id='searchform' action='addToList.jsp'>");
                                    out.println("<input type='hidden' value=" + viewBookBean.getISBN() + " name='ISBN' id='ISBN' />");
                                    out.println(" or <a href='' onclick='document.addList" + viewBookBean.getISBN() + ".submit(); return false;'>Add Book To Wishlist</a>");
                                    out.println("</form>");
                                }
                                else{
                                    out.println("<b> Book will be removed from Wishlist!</b>");
                                }
                            }
                            else{
                                out.println("<b>Book in Cart!</b>");
                            }
                            
                           /* out.println("<hr/><p>Feedback Statistics:</p>");
                            try {
                            FeedbackStatistics stats = feedClient.getFeedbackStatistics(Integer.parseInt(viewBookBean.getISBN()));
                                out.println("Average rating: " = stats.getAverageRating());
                            }
                            catch (NoFeedbackFoundException_Exception ex) {
                                out.println("No feedback found");
                                }
                            */
                            String statusL = "";

                            if(session.getAttribute( "statusMessageL" ) != null){
                                statusL = (String) session.getAttribute( "statusMessageL" );
                            }

                            out.println("<hr/><p>Give Feedback about book:</p>");
                            if( ! statusL.contains("successful")){
                                out.println("Please login/register to give feedback for this book!");
                            }
                            else{
                     
                                out.println("<font color='red'>Feedback Service is currently down, please try again later!</font>");
                            }

                            out.println("<hr/><p>Other users feedback:</p>");
                            out.println("<font color='red'>Feedback Service is currently down, please try again later!</font><hr/>");
                            %>
                	</div><!-- .entry-content -->
		</div><!-- #post-0 -->

                
	</div><!-- #content -->
</div><!-- #container -->

<%@ include file="sidebar.jsp" %>
	
<!-- /Content -->

<%@ include file="footer.jsp" %>